<?php
    session_start();

    $file_name="tmp/eleve_".$_GET['id'].".csv";

    header('Content-Type: application/octet-stream; name="'.$file_name.'"');
    header('Location: '.$file_name.'');
    header('Date: '.$date);
    header('Expires: '.gmdate(DATE_RFC1123, time()+1));

    if ($_SESSION['user']['FK_statut'] != 1) 
    {
        $_SESSION['user']['message'] = "Vous n'avez pas les droits pour acceder à la page ".$_SERVER['REQUEST_URI'];
    }
    else {
        unset($_SESSION['user']['message']);

        include "../conf/config.php";

        $db = mysql_connect($host, $user_select, $mdp_select);
        $db_selected = mysql_select_db('website', $db);

        $fp = fopen("tmp/eleve_".$_GET['id'].".csv", "w");

        $requete = "SELECT user.Nom, user.Prenom, comp.nomComp, evo.skill_validate, evo.date_modif 
                    FROM comp, evo, user
                    WHERE evo.id_user=".$_GET['id']."
                    AND evo.id_comp=comp.id_comp
                    AND evo.id_user=user.id_user";

        $sql = mysql_query($requete);

        if(mysql_num_rows($sql) > 0)
        {
            $Row = mysql_fetch_array($sql);
            $t = array($Row['Nom'],$Row['Prenom']) ;
            fputcsv($fp,$t);

            $Desi = array(utf8_decode("Nom de la compétence"), "Niveau de progression", "Date de modification");

            fputcsv($fp, $Desi);

            while ($Row = mysql_fetch_array($sql)) 
            {
                fputcsv($fp, array(utf8_decode($Row['nomComp']), $Row['skill_validate'], $Row['date_modif']));
            }

            fclose($fp);

            unlink("tmp/".$file_name);

            $_SESSION['user']['message'] = "Export réalisé avec succès.";
        }
        else
        {
            $_SESSION['user']['message'] = "Aucune donnée à enregistrer.";
        }

    }
    
       
?>